﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.Diagnostics;
using System.Web.UI;

namespace SQLDropDownListFilter.WebPartCode
{
    public static class Extensions
    {
        public static void LogToSharepoint(this Exception ex, bool Critical)
        {
            if(Critical)
                ex.ToString().LogToSharePoint(PortalLogLevel.Critical, true);
            else
                ex.ToString().LogToSharePoint(PortalLogLevel.Unexpected, true);
        }

        public static void LogToSharePoint(this string message, PortalLogLevel logLevel, bool isError)
        {
            string MessageTemplate = string.Empty;
            switch (logLevel)
            {
                case PortalLogLevel.Critical:
                    if (isError)
                        MessageTemplate = "Critical Error: {0}";
                    else
                        MessageTemplate = "Critical Info: {0}";

                    break;
                case PortalLogLevel.Information:
                    MessageTemplate = "Info: {0}";
                    break;
                case PortalLogLevel.Unexpected:
                    if(isError)
                        MessageTemplate = "Unexpected Error: {0}";
                    else
                        MessageTemplate = "Unexpected Message: {0}";
                    break;
                case PortalLogLevel.Verbose:
                    MessageTemplate = "Verbose Info: {0}";
                    break;
                default:
                    MessageTemplate = "{0}";
                    break;
            }
            PortalLog.LogString(MessageTemplate, message);
        }

        public static void AddLiteral(this ControlCollection controls, string html)
        {
            controls.Add(new LiteralControl(html));
        }
    }
}
